package com.tth.learn.addproperties.exer;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @Author tth
 * @Date 2022/12/13 18:04
 */
public class TreeSetExer {
    @Test
    public void test1() {
        TreeSet treeSet = new TreeSet();
        treeSet.add(new Employee("Jack", 33, new MyDate(1989, 1, 21)));
        treeSet.add(new Employee("Janny", 28, new MyDate(1994, 11, 2)));
        treeSet.add(new Employee("Steven", 55, new MyDate(1967, 2, 28)));
        treeSet.add(new Employee("Anna", 22, new MyDate(2000, 4, 15)));
        treeSet.add(new Employee("Vei", 33, new MyDate(1989, 12, 21)));

        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    @Test
    public void test2() {
        Comparator com = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee && o1 instanceof Employee) {
                    Employee e1 = (Employee) o1;
                    Employee e2 = (Employee) o2;
                    MyDate birthday1 = e1.getBirthday();
                    MyDate birthday2 = e2.getBirthday();
                    int compare = Integer.compare(birthday1.getYear(), birthday2.getYear());
                    if (compare == 0) {
                        compare = Integer.compare(birthday1.getMonth(), birthday2.getMonth());
                        if (compare == 0) {
                            compare = Integer.compare(birthday1.getDay(), birthday2.getDay());
                        }
                    }
                    return compare;
                }

                throw new RuntimeException("数据格式不匹配");
            }
        };

        TreeSet treeSet = new TreeSet(com);
        treeSet.add(new Employee("Jack", 33, new MyDate(1989, 1, 21)));
        treeSet.add(new Employee("Janny", 28, new MyDate(1994, 11, 2)));
        treeSet.add(new Employee("Steven", 55, new MyDate(1967, 2, 28)));
        treeSet.add(new Employee("Anna", 22, new MyDate(2000, 4, 15)));
        treeSet.add(new Employee("Vei", 33, new MyDate(1989, 12, 21)));

        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

class Employee implements Comparable{
    private String name;
    private int age;
    private MyDate birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Employee) {
            Employee employee = (Employee) o;
            return this.name.compareTo(employee.name);
        }

        throw new RuntimeException("数据类型不匹配");
    }
}

class MyDate {
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}

